package com.jxbeidou.system.menu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.menu.convert.RoleDataScopeConvert;
import com.jxbeidou.system.menu.entity.RoleDataScopeDO;
import com.jxbeidou.system.menu.query.RoleDataScopeQuery;
import com.jxbeidou.system.menu.vo.RoleDataScopeVO;
import com.jxbeidou.system.menu.mapper.RoleDataScopeMapper;
import com.jxbeidou.system.menu.service.RoleDataScopeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色数据权限表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class RoleDataScopeServiceImpl extends BaseServiceImpl<RoleDataScopeMapper, RoleDataScopeDO> implements RoleDataScopeService {

    @Override
    public PageResult<RoleDataScopeVO> page(RoleDataScopeQuery query) {
        IPage<RoleDataScopeDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<RoleDataScopeVO> result = PageResultConvert.convert(page, entityList -> RoleDataScopeConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<RoleDataScopeDO> getWrapper(RoleDataScopeQuery query){
        LambdaQueryWrapper<RoleDataScopeDO> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(RoleDataScopeVO vo) {
        RoleDataScopeDO entity = RoleDataScopeConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(RoleDataScopeVO vo) {
        RoleDataScopeDO entity = RoleDataScopeConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}